package com.htc.android.footprints.service;

import java.util.ArrayList;
import java.util.Iterator;

import android.content.Context;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.htc.android.footprints.utils.GpsFixStatus;

public class LocationAgent {
	private static final long FALLBACK_PROVIDER_THRESHOLD_MILLIS = 10000L;
	private static final String GPS = "gps";
	private static final int HANDLER_NOTIFY_CLIENT = 104;
	private static final int HANDLER_TRACKING_TIMEOUT = 100;
	private static final int HANDLER_UPDATE_STATUS = 103;
	private static final int HANDLER_WAIT_FOR_PAUSE = 105;
	private static final int ICON_LEVEL_GPS_OFF = 6;
	private static final int ICON_LEVEL_GPS_ON = 5;
	private static final int ICON_LEVEL_MAP = 7;
	private static final int ICON_LEVEL_NETWORK = 8;
	private static final int ICON_LEVEL_SIGNAL0 = 0;
	private static final int ICON_LEVEL_SIGNAL1 = 1;
	private static final int ICON_LEVEL_SIGNAL2 = 2;
	private static final int ICON_LEVEL_SIGNAL3 = 3;
	private static final int ICON_LEVEL_SIGNAL4 = 4;
	private static final float LOCATION_MIS_DISTANCE = 0.0F;
	private static final long LOCATION_UPDATE_MILLIS = 0L;
	private static final int LOW_SIGNAL = 5;
	private static final int MODE_FIX_GPS = 13;
	private static final int MODE_FIX_NETWORK = 14;
	private static final int MODE_PAUSE_STANDBY = 15;
	private static final int MODE_PAUSE_WAIT_TIMEOUT = 16;
	private static final int MODE_STOP = 10;
	private static final int MODE_TIMEOUT = 12;
	private static final int MODE_TRACKING = 11;
	private static final String NETWORK = "network";
	private static String[] PROVIDER_NAMES;
	private static final String TAG = "LocationAgent";
	private static final long TIMEOUT = 120000L;
	private static final long WAIT_PAUSE_TIMER = 5000L;

	private static LocationAgent mInstance;

	private GpsFixStatus gpsFixStatus = null;

	private GpsStatus.Listener gpsListener = new GpsStatus.Listener() {
		GpsStatus gstatus = null;

		public void onGpsStarted() {
		}

		public void onGpsStatusChanged(int paramInt) {
			if (paramInt == 3)
				if (LocationAgent.this.mLocation == null)
					Log.d("LocationAgent", "GpsStatusListener.onFirstFix");
			while (true) {
				return;
				if (paramInt == 4) {
					this.gstatus = LocationAgent.this.mLocationManager
							.getGpsStatus(this.gstatus);
					if (LocationAgent.this.mCurMode == 10)
						continue;
					float f = 0.0F;
					int i = 0;
					Iterator localIterator = this.gstatus.getSatellites()
							.iterator();
					while (localIterator.hasNext()) {
						f += ((GpsSatellite) localIterator.next()).getSnr();
						i++;
					}
					if (i > 0) {
						LocationAgent.access$702(LocationAgent.this, f / i);
						Log.i("LocationAgent", "Gps Signal = "
								+ LocationAgent.this.mGpsSignal);
						continue;
					}
					LocationAgent.access$702(LocationAgent.this, 0.0F);
					Log.i("LocationAgent", "statellite count = 0");
					continue;
				}
			}
		}

		public void onGpsStopped() {
		}
	};
	private boolean isTimeoutNotify = true;
	private boolean isTrackingAfterResume = false;
	private AgentListener mClientListener = null;
	private Context mContext = null;
	private int mCurIcon = 6;
	private int mCurMode = 10;
	private final ArrayList<NameAndDate> mEnabledProviders = new ArrayList(2);
	private float mGpsSignal = 0.0F;
	private Handler mHandler = new Handler() {
		public void handleMessage(Message paramMessage) {
			switch (paramMessage.what) {
			case 101:
			case 102:
			default:
			case 103:
			case 100:
			case 105:
			case 104:
			}
			while (true) {
				return;
				Log.i("LocationAgent", "HANDLER_UPDATE_STATUS");
				continue;
				Log.d("LocationAgent", "TRACKING_TIMEOUT, isTimeoutNotify = "
						+ LocationAgent.this.isTimeoutNotify);
				Log.d("LocationAgent", "TRACKING_TIMEOUT, mCurMode = "
						+ LocationAgent.this.mCurMode);
				if (LocationAgent.this.mCurMode == 11) {
					if (LocationAgent.this.isTimeoutNotify) {
						LocationAgent.access$102(LocationAgent.this, 12);
						Log.d("LocationAgent",
								"TRACKING_TIMEOUT, mCurMode = MODE_TIMEOUT");
						continue;
					}
					LocationAgent.this.startTimer();
					continue;
				}
				if (LocationAgent.this.mCurMode == 15) {
					Log.w("LocationAgent", "MODE_PAUSE_STANDBY  timeout !!!");
					LocationAgent.access$1002(LocationAgent.this, true);
					LocationAgent.this.stopTrackingWithoutNotify();
					continue;
				}
				if (LocationAgent.this.mCurMode != 16)
					continue;
				Log.d("LocationAgent", "MODE_PAUSE_WAIT_TIMEOUT");
				LocationAgent.access$1002(LocationAgent.this, true);
				LocationAgent.this.stopTrackingWithoutNotify();
				continue;
				Log.w("LocationAgent", "HANDLER_WAIT_FOR_PAUSE");
				if (LocationAgent.this.mCurMode != 15)
					continue;
				LocationAgent.access$102(LocationAgent.this, 16);
				continue;
				LocationAgent.this.notifyClient();
			}
		}
	};
	private Location mLocation = null;
	private LocationListener mLocationListener = new LocationListener() {
		/** @deprecated */
		public void onLocationChanged(Location paramLocation) {
			while (true) {
				String str;
				long l1 = SystemClock.elapsedRealtime();
				long l2 = l1 - 10000L;
				str = paramLocation.getProvider();
				Iterator localIterator = LocationAgent.this.mEnabledProviders
						.iterator();
				if (!localIterator.hasNext())
					continue;
				LocationAgent.NameAndDate localNameAndDate = (LocationAgent.NameAndDate) localIterator
						.next();
				if (!localNameAndDate.name.equals(str))
					continue;
				localNameAndDate.date = l1;
				if (LocationAgent.this.mCurMode == 10)
					continue;
				int i = LocationAgent.this.mCurMode;
				if (i == 12) {
					return;
					if ((!localNameAndDate.name.equals("gps"))
							|| (localNameAndDate.date <= l2))
						continue;
					Log.i("LocationAgent",
							"Got fallback update soon after preferred udpate, ignoring");
					continue;
				}
				if (str.equals("gps")) {
					if (LocationAgent.this.mLocation == null)
						LocationAgent.access$202(LocationAgent.this,
								new Location("gps"));
					LocationAgent.this.mLocation.set(paramLocation);
					LocationAgent.access$102(LocationAgent.this, 13);
					LocationAgent.this.removeTimer();
					Log.d("LocationAgent", "Fix GPS, MODE_FIX_GPS");
					continue;
				}
				if (!str.equals("network"))
					continue;
				Log.i("LocationAgent", "Fix Networ");
				if (LocationAgent.this.mLocationNetwork == null)
					LocationAgent.access$402(LocationAgent.this, new Location(
							"network"));
				LocationAgent.this.mLocationNetwork.set(paramLocation);
				if (LocationAgent.this.mClientListener == null)
					continue;
				LocationAgent.this.mClientListener.onUpdateStatus(14, 6,
						LocationAgent.this.mLocationNetwork.getLatitude(),
						LocationAgent.this.mLocationNetwork.getLongitude());
				Log.i("LocationAgent", "notify client : MODE_FIX_NETWORK");
			}
		}

		public void onProviderDisabled(String paramString) {
		}

		public void onProviderEnabled(String paramString) {
		}

		public void onStatusChanged(String paramString, int paramInt,
				Bundle paramBundle) {
		}
	};
	private LocationManager mLocationManager = null;
	private Location mLocationNetwork = null;
	private long mTimeout = 120000L;

	static {
		String[] arrayOfString = new String[2];
		arrayOfString[0] = "gps";
		arrayOfString[1] = "network";
		PROVIDER_NAMES = arrayOfString;
		mInstance = null;
	}

	private LocationAgent(Context paramContext) {
		this.mContext = paramContext;
		Log.i("LocationAgent", "mContext = " + this.mContext);
		init();
	}

	private int getGpsSignalLevel() {
		int i = 0;
		if (this.mGpsSignal < 5.0F)
			i = 0;
		while (true) {
			return i;
			if ((this.mGpsSignal >= 5.0F) && (this.mGpsSignal < 10.0F)) {
				i = 1;
				continue;
			}
			if ((this.mGpsSignal > 10.0F) && (this.mGpsSignal < 20.0F)) {
				i = 2;
				continue;
			}
			if ((this.mGpsSignal > 20.0F) && (this.mGpsSignal < 30.0F)) {
				i = 3;
				continue;
			}
			if (this.mGpsSignal <= 30.0F)
				continue;
			i = 4;
		}
	}

	private void init() {
		Log.i("LocationAgent", "mContext = " + this.mContext);
		this.mLocationManager = ((LocationManager) this.mContext
				.getSystemService("location"));
		resetStatus();
	}

	public static LocationAgent instance(Context paramContext) {
		if (mInstance == null)
			mInstance = new LocationAgent(paramContext);
		return mInstance;
	}

	private boolean isProviderEnabled(String paramString) {
		return this.mLocationManager.isProviderEnabled(paramString);
	}

	private void notifyClient() {
		if (this.mClientListener != null)
			switch (this.mCurMode) {
			// private static final int MODE_FIX_GPS = 13;
			// private static final int MODE_FIX_NETWORK = 14;
			// private static final int MODE_PAUSE_STANDBY = 15;
			// private static final int MODE_PAUSE_WAIT_TIMEOUT = 16;
			// private static final int MODE_STOP = 10;
			// private static final int MODE_TIMEOUT = 12;
			// private static final int MODE_TRACKING = 11;
			// private static final String NETWORK = "network";
			case 14:
			default:
			case 10:
			case 11:
			case 15:
			case 16:
			case 12:
			case 13:
			}
		while (true) {
			return;
			this.mClientListener.onUpdateStatus(this.mCurMode, this.mCurIcon,
					0.0D, 0.0D);
			Log.i("LocationAgent", "notify client : MODE_STOP");
			continue;
			this.mCurIcon = getGpsSignalLevel();
			this.mClientListener.onUpdateStatus(this.mCurMode, this.mCurIcon,
					0.0D, 0.0D);
			this.mHandler.sendEmptyMessageDelayed(104, 1000L);
			Log.i("LocationAgent", "notify client : MODE_TRACKING");
			continue;
			this.mCurIcon = getGpsSignalLevel();
			this.mClientListener.onUpdateStatus(11, this.mCurIcon, 0.0D, 0.0D);
			this.mHandler.sendEmptyMessageDelayed(104, 1000L);
			Log.i("LocationAgent",
					"notify client : MODE_TRACKING (MODE_PAUSE_STANDBY)");
			continue;
			pause();
			continue;
			this.mCurIcon = getGpsSignalLevel();
			this.mClientListener.onUpdateStatus(this.mCurMode, this.mCurIcon,
					0.0D, 0.0D);
			Log.i("LocationAgent", "notify client : MODE_TIMEOUT");
			continue;
			double d1 = this.mLocation.getLatitude();
			double d2 = this.mLocation.getLongitude();
			this.mCurIcon = 5;
			this.mClientListener.onUpdateStatus(this.mCurMode, this.mCurIcon,
					d1, d2);
			Log.i("LocationAgent", "notify client : MODE_FIX_GPS");
		}
	}

	private void pause() {
		Log.d("LocationAgent", "Agent pause");
		if (this.mClientListener != null)
			this.mClientListener.onAgentPause();
	}

	private void removeSystemListener() {
		Log.i("LocationAgent", "removeSystemListener");
		this.mLocationManager.removeGpsStatusListener(this.gpsListener);
		this.mLocationManager.removeUpdates(this.mLocationListener);
	}

	private void removeTimer() {
		Log.d("LocationAgent", "removeTimer");
		if (this.mHandler.hasMessages(100))
			this.mHandler.removeMessages(100);
	}

	private void requestGPS() {
		Log.i("LocationAgent", "requestGPS");
		this.mLocationManager.requestLocationUpdates("gps", 0L, 0.0F,
				this.mLocationListener);
	}

	private void requestNetwork() {
		Log.i("LocationAgent", "requestNetwork");
		this.mLocationManager.requestLocationUpdates("network", 0L, 0.0F,
				this.mLocationListener);
	}

	private void resetStatus() {
		Log.i("LocationAgent", "resetStatus");
		this.isTimeoutNotify = true;
		this.mLocation = null;
		this.mLocationNetwork = null;
		this.mGpsSignal = 0.0F;
		this.mEnabledProviders.clear();
	}

	private void setGpsFixListener() {
		this.gpsFixStatus.setFixListener(new GpsFixStatus.FixListener() {
			public void onFix() {
			}
		});
	}

	private void startTimer() {
		Log.d("LocationAgent", "startTimer");
		this.mHandler.sendEmptyMessageDelayed(100, this.mTimeout);
	}

	public void continueTracking() {
		Log.d("LocationAgent", "continueTracking, mCurMode = " + this.mCurMode);
		removeTimer();
		startTimer();
		this.mCurMode = 11;
		if (this.mHandler.hasMessages(104))
			this.mHandler.removeMessages(104);
		notifyClient();
	}

	public void enableMyLocation() {
		Log.d("LocationAgent", "enableMyLocation, MODE_TRACKING");
		Log.d("LocationAgent", "gps location: " + this.mLocation
				+ ", network location: " + this.mLocationNetwork);
		if (this.mCurMode == 11) {
			if (this.mLocation != null)
				Log.d("LocationAgent",
						"enableMyLocation when mCurMode is MODE_TRACKING. but mLocation is not null");
			return;
		}
		this.mCurMode = 11;
		this.mLocationNetwork = null;
		this.mLocation = null;
		this.mLocationManager.addGpsStatusListener(this.gpsListener);
		String[] arrayOfString = PROVIDER_NAMES;
		int i = arrayOfString.length;
		for (int j = 0;; j++)
			if (j < i) {
				String str = arrayOfString[j];
				try {
					if (!isProviderEnabled(str))
						continue;
					this.mEnabledProviders.add(new NameAndDate(str));
					if (str.equals("gps"))
						requestGPS();
					if (str.equals("network"))
						requestNetwork();
					Log.i("LocationAgent", "Request updates from " + str);
				} catch (SecurityException localSecurityException) {
					Log.w("LocationAgent", "Couldn't get provider " + str
							+ ": " + localSecurityException.getMessage());
				} catch (IllegalArgumentException localIllegalArgumentException) {
					Log.w("LocationAgent", "Couldn't get provider " + str
							+ ": " + localIllegalArgumentException.getMessage());
				}
			} else {
				startTimer();
				this.mHandler.sendEmptyMessageDelayed(104, 1000L);
				break;
			}
	}

	public void free() {
		if (this.mHandler.getLooper() != Looper.getMainLooper())
			this.mHandler.getLooper().quit();
		this.mClientListener = null;
	}

	public GeoPoint getFixGpsCeoPoint() {
		GeoPoint localGeoPoint = null;
		if (this.mLocation != null)
			localGeoPoint = new GeoPoint(
					(int) (1000000.0D * this.mLocation.getLatitude()),
					(int) (1000000.0D * this.mLocation.getLongitude()));
		return localGeoPoint;
	}

	public Location getFixGpsLocation() {
		return this.mLocation;
	}

	public GeoPoint getFixNetworkGeoPoint() {
		GeoPoint localGeoPoint = null;
		if (this.mLocationNetwork != null)
			localGeoPoint = new GeoPoint(
					(int) (1000000.0D * this.mLocationNetwork.getLatitude()),
					(int) (1000000.0D * this.mLocationNetwork.getLongitude()));
		return localGeoPoint;
	}

	public Location getFixNetworkLocation() {
		return this.mLocationNetwork;
	}

	public boolean isTrackingGps() {
		Log.i("LocationAgent", "isTrackingGps: mCurMode = " + this.mCurMode);
		if (this.mCurMode == 11)
			;
		for (int i = 1;; i = 0)
			return i;
	}

	public boolean isTrackingTimeout() {
		Log.i("LocationAgent", "isTrackingTimeout: mCurMode = " + this.mCurMode);
		if (this.mCurMode == 12)
			;
		for (int i = 1;; i = 0)
			return i;
	}

	public void resume() {
		Log.d("LocationAgent", "Agent resume, isTrackingAfterResume = "
				+ this.isTrackingAfterResume);
		this.mHandler.removeMessages(105);
		if (this.mCurMode == 15) {
			this.mCurMode = 11;
			Log.w("LocationAgent", "resume, mCurMode = MODE_TRACKING");
		}
		if (this.isTrackingAfterResume)
			enableMyLocation();
		this.isTrackingAfterResume = false;
	}

	public void setAgentListener(AgentListener paramAgentListener) {
		this.mClientListener = paramAgentListener;
	}

	public void setTimeout(Long paramLong) {
		this.mTimeout = paramLong.longValue();
		removeTimer();
		startTimer();
	}

	public void setTimeoutNotify(boolean paramBoolean) {
		this.isTimeoutNotify = paramBoolean;
	}

	public void shutdownAgent() {
		Log.d("LocationAgent", "shutdownAgent, MODE_STOP");
		this.mCurMode = 10;
		this.mHandler.removeMessages(104);
		removeTimer();
		resetStatus();
		this.mTimeout = 120000L;
		removeSystemListener();
		notifyClient();
	}

	public void stopTracking() {
		Log.d("LocationAgent", "stopTracking, MODE_STOP");
		this.mCurMode = 10;
		removeTimer();
		resetStatus();
		removeSystemListener();
		notifyClient();
	}

	public void stopTrackingWithoutNotify() {
		Log.d("LocationAgent", "stopTrackingWithoutNotify, MODE_STOP");
		this.mCurMode = 10;
		removeTimer();
		resetStatus();
		removeSystemListener();
	}

	public void waitForPause() {
		if (this.mCurMode == 11) {
			this.mCurMode = 15;
			this.mHandler.sendEmptyMessageDelayed(105, 5000L);
			Log.w("LocationAgent",
					"waitForPause, mCurMode = MODE_PAUSE_STANDBY");
		}
	}

	private static class NameAndDate {
		public long date;
		public String name;

		public NameAndDate(String paramString) {
			this.name = paramString;
			this.date = -9223372036854775808L;
		}

		public boolean equals(Object paramObject) {
			if (hashCode() == paramObject.hashCode())
				;
			for (int i = 1;; i = 0)
				return i;
		}

		public int hashCode() {
			return this.name.hashCode();
		}
	}

	public static abstract interface AgentListener {
		public abstract void onAgentPause();

		public abstract void onUpdateStatus(int paramInt1, int paramInt2,
				double paramDouble1, double paramDouble2);
	}
}

/*
 * Location:
 * E:\workspace\dex2java\dex-translator-0.0.9.3\HtcFootprints_dex2jar.jar
 * Qualified Name: com.htc.android.footprints.service.LocationAgent JD-Core
 * Version: 0.6.0
 */